package com.wish.group05.business.controller;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.Business;
import com.wish.group05.base.entity.SysRegion;
import com.wish.group05.base.entity.SysUser;
import com.wish.group05.base.service.IBusinessService;
import com.wish.group05.base.service.ISysRegionService;
import com.wish.group05.base.service.ISysUserService;
import com.wish.group05.business.vo.BusinessVo;
import com.wish.group05.common.core.utils.StringUtils;
import com.wish.group05.common.core.web.domain.AjaxResult;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.page.TableDataInfo;

import javax.swing.plaf.synth.Region;

/**
 * 商户Controller
 * 
 * @author wish
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/business")
public class TbBusinessController extends BaseController
{
    @Autowired
    private IBusinessService iBusinessService;

    @Autowired
    private ISysRegionService sysRegionService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询商户列表
     */
    @PreAuthorize(hasPermi = "system:business:list")
    @GetMapping("/list")
    public TableDataInfo list(Business business)
    {
        startPage();
        List<Business> list = iBusinessService.list();
        return getDataTable(list);
    }

    /**
     * 通过id查询商户表
     */
    @GetMapping(value = "/getBusiness/{id}")
    public AjaxResult getBusiness(@PathVariable("id") Integer id)
    {
        //根据商户id获取商户信息
        Business business = iBusinessService.getById(id);
        //copy属性
        BusinessVo businessVo = new BusinessVo();
        BeanUtils.copyProperties(business,businessVo);

        //根据城市表获取具体城市名称
        if(businessVo.getProvince()!=null){
            SysRegion province = getCityByID(businessVo.getProvince());
            businessVo.setProvinceName(province.getRegionName());
        }
        if(businessVo.getCity()!=null) {
            SysRegion city = getCityByID(businessVo.getCity());
            businessVo.setCityName(city.getRegionName());
        }
        if(businessVo.getCounty()!=null) {
            SysRegion country = getCityByID(businessVo.getCounty());
            businessVo.setCountryName(country.getRegionName());
        }


        //根据用户id获取管理员名称
        if(businessVo.getUserId()!=null){
            SysUser sysUser = sysUserService.getById(businessVo.getUserId());
            businessVo.setUserName(sysUser.getUsername());
        }

        System.err.println(businessVo);
        return AjaxResult.success(businessVo);
    }

    /**
     * 审核通过
     * @return
     */
    @PostMapping("pass/{id}")
    public AjaxResult pass(@PathVariable("id")Integer id){
        //根据id查询
        Business business = iBusinessService.getById(id);
        //修改审核状态
        business.setStatus(1);
        iBusinessService.saveOrUpdate(business);
        return AjaxResult.success();
    }

    /**
     * 审核未通过
     * @return
     */
    @PostMapping("noPass/{id}")
    public AjaxResult noPass(@PathVariable("id")Integer id){
        //根据id查询
        Business business = iBusinessService.getById(id);
        //修改审核状态
        business.setStatus(2);
        iBusinessService.saveOrUpdate(business);
        return AjaxResult.success();
    }

    /**
     * 查询城市表
     */
    private SysRegion getCityByID(Integer id){
        SysRegion sysRegion = sysRegionService.getOne(new QueryWrapper<SysRegion>().eq("REGION_ID", id));
        return sysRegion;
    }

//    /**
//     * 导出商户列表
//     */
//    @PreAuthorize(hasPermi = "system:business:export")
//    @Log(title = "商户", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, TbBusiness tbBusiness) throws IOException
//    {
//        List<TbBusiness> list = tbBusinessService.selectTbBusinessList(tbBusiness);
//        ExcelUtil<TbBusiness> util = new ExcelUtil<TbBusiness>(TbBusiness.class);
//        util.exportExcel(response, list, "business");
//    }
//
//    /**
//     * 获取商户详细信息
//     */
//    @PreAuthorize(hasPermi = "system:business:query")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return AjaxResult.success(tbBusinessService.selectTbBusinessById(id));
//    }
//
//    /**
//     * 新增商户
//     */
//    @PreAuthorize(hasPermi = "system:business:add")
//    @Log(title = "商户", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody TbBusiness tbBusiness)
//    {
//        return toAjax(tbBusinessService.insertTbBusiness(tbBusiness));
//    }
//
//    /**
//     * 修改商户
//     */
//    @PreAuthorize(hasPermi = "system:business:edit")
//    @Log(title = "商户", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody TbBusiness tbBusiness)
//    {
//        return toAjax(tbBusinessService.updateTbBusiness(tbBusiness));
//    }
//
//    /**
//     * 删除商户
//     */
//    @PreAuthorize(hasPermi = "system:business:remove")
//    @Log(title = "商户", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(tbBusinessService.deleteTbBusinessByIds(ids));
//    }
}
